<?php
namespace frontend\controllers;
header('content-type:text/html;charset=utf-8');
use Yii;
use yii\web\Controller;
use frontend\models\register\RegisterForm;
use frontend\models\login\LoginForm;
use frontend\models\User;
use frontend\models\code\Code;
use backend\models\system\SysConfig;
use common\components\Easemob;
use backend\models\article\Page;
class IndexController extends Controller
{
	public $enableCsrfValidation = false; 
	
	/*
	*	注册
	*/

	public function actionRegister()
    {
		$code = new Code();
		$request = Yii::$app->request;
		$register = new RegisterForm();
        
        //ajax验证唯一
        
        $register->load($_POST);
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return \yii\bootstrap\ActiveForm::validate($register);
        }

		$register->attributes = $request->post('RegisterForm');
		if(!$register->validate()){
			echo "格式不对";
			
		}else{
			
			$data = $request->post('RegisterForm');
			$user_code = $code->getUsercode($data['user_email_phone']);
			if($user_code=="" || $user_code!=$data['user_code']){
				echo "<script>alert('清时输入正确的验证码');location.href='".\Yii::$app->urlManager->createUrl('site/index')."'</script>";die;
			}
			$data['user_regtime'] = time();
			unset($data['user_qrpwd']);
			if(preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",$data['user_email_phone'])){
				$data['user_email']=$data['user_email_phone'];
			}
			if(preg_match("/^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/",$data['user_email_phone'])){
				$data['user_phone']=$data['user_email_phone'];
			}
			unset($data['user_email_phone']);
			$sysconfig = new SysConfig();
			$sysconfig->config_key = "default_photo";
			$re = $sysconfig->getValueBykey();
			$data['user_thumb'] = $re['config_value'];
			$data['user_pwd'] = md5($data['user_pwd']);
			unset($data['user_code']);
			$data['user_emid'] = md5($data['user_pwd']);
			$data['user_regip'] = Yii::$app->request->userIP;
			$user = new User;
			$user->attributes = $data;
			if($user->Save()){
				$user_id = $user->db->getLastInsertID();
				$user->user_num='11'.$user_id;
				$user->find()->where(['user_id'=>$user_id])->asarray()->one();
				$user->save();
				$em = new Easemob();
				$options['username']='11'.$user_id;
				$options['password']=$data['user_emid'];
				$re = $em->openRegister($options);
				$session = Yii::$app->session;
				$session->open();
				$session->set('user_id',$user_id);
				$session->set('user_thumb',$data['user_thumb']);
				$session->set('user_emid',$data['user_emid']);
				$session->set('user_num','11'.$user_id);
				$session['captcha'] = [
					'number' => 5,
					'lifetime' => 3600,
				];
				$session->close();
				echo "<script>alert('注册成功');location.href='".\Yii::$app->urlManager->createUrl('site/index')."'</script>";
			}
		}
    }
	/*
	*	发送验证码
	*/
	public function actionCodes()
    {
		$code = new Code();
		$request = Yii::$app->request;
		$email_phone = $request->post('email_phone');
		echo $code->getCode($email_phone);
		
    }
	/*
	* 用户是否存在
	*/
	public function actionGetemailphone()
	{
		$request = Yii::$app->request;
		$email_phone = $request->post('email_phone');
		$user = User::find()->where(['or',['user_phone'=>$email_phone],['user_email'=>$email_phone]])->one();
		if($user){
			echo 1;
		}
	}
	/*
	* 获取用户协议
	*/
	public function actionServicecontent()
	{
		$this->redirect(['site/single','url'=>'clause']);
	}

    
	/*
	*	用户登录
	*/
	public function actionLogin()
	{
        $request = Yii::$app->request;
        $login = new LoginForm();
        $login->attributes = $request->post('LoginForm');
        
        if (Yii::$app->request->isAjax && $login->validate()) {
            $data = $request->post('LoginForm');
            $re = $login->getUserName($data['user_name']);
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            if($re)
            {
                if($re['user_pwd']==md5($data['user_pwd'])){
                    if($data['rememberMe']==1){
                        
                        ini_set('session.cookie_lifetime',3600*24*30);
                    }
                    $session = Yii::$app->session;
                    $session->set('user_id',$re['user_id']);
                    $session->set('user_thumb',$re['user_thumb']);
                    $session->set('user_num',$re['user_num']);
                    $session->set('user_emid',$re['user_emid']);

                    return [
                        'code' => '200',
                        'msg' => '登陆成功！',
                    ];
                }else{
                    return [
                        'code' => '400',
                        'msg' => '用户名或密码错误',
                    ];
                }
            }else{
                return [
                    'code' => '400',
                    'msg' => '用户名或密码错误',
                ];
            }
        } else {
            $this->redirect(['site/index']);
        }
	}
	/*
	*	用户退出
	*/
	public function actionLoginout()
	{
		$session = Yii::$app->session;
		$cookies = Yii::$app->response->cookies;
		$session->remove('user_id');;
		$session->remove('user_thumb');
		$session->remove('user_num');;
		$session->remove('user_emid');;
		if(!empty(Yii::$app->request->cookies->getValue('user_id'))){
			$cookies->remove('user_id');
			$cookies->remove('user_thumb');
			$cookies->remove('user_num');
			$cookies->remove('user_emid');
		}
        $this->redirect(['site/index']);
	}

}